<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/10/18 10:42
 * Desc: 红包
 */
class Service_Api_Envelope extends Contract_Api_Envelope
{
    /**
     * 获取用户当前产品可用红包数量&列表
     * @param $mid
     * @param $type_id
     * @return array
     */
    public function get_enable_envelope($mid, $type_id): array
    {
        return array(
            'list' => Model_Order_Envelope::get_book_envelope($type_id, $mid)
        );
    }

    /**
     * 获取会员红包
     * @param $mid
     * @param array $params
     * @return array
     */
    public function get_member_envelope($mid, array $params = array()): array
    {
        //状态0-全部，1-未使用 2- 已使用
        $status   = Arr::get($params, 'status', 0);
        $page     = Arr::get($params, 'page');
        $pagesize = Arr::get($params, 'pagesize', 10);
        $offset   = St_Page::offset($page, $pagesize);

        $builder = DB::select('id', 'money', 'use', 'typeids')
            ->from('envelope_member')
            ->where('memberid', '=', $mid);

        if ($status == 1) {
            $builder->where('use', '=', 0);
        } elseif ($status == 2) {
            $builder->where('use', '=', 1);
        }

        $total = $builder->execute()->count();

        $list = $builder->order_by('id', 'DESC')
            ->offset($offset)
            ->limit($pagesize)
            ->execute()
            ->as_array();

        $model_api_v2_envelope = new Model_Api_V2_Envelope();
        foreach ($list as &$v) {
            $v['money']        = Currency_Tool::price($v['money']);
            $v['module_title'] = $model_api_v2_envelope->get_envelope_module_title($v['typeids']);

            unset($v['typeids']);
        }

        return array(
            'total' => $total,
            'list' => $list
        );
    }

    /**
     * 获取红包说明
     * @return array
     */
    public function get_envelope_description(): array
    {
        return array(
            'envelope_description' => Model_Sysconfig::get_sys_conf('value', 'cfg_envelope_description')
        );
    }

    /**
     * 支付成功获取订单的分享红包信息
     * @param $order_sn
     * @return array
     */
    public function get_order_envelope_info($order_sn): array
    {
        $info = Model_Order_Envelope::order_show($order_sn);
        return array(
            'envelope_info' => $info
        );
    }

    /**
     * 领取红包
     * @param $mid
     * @param $envelope_order_id
     * @return array
     * @throws Exception
     */
    function receive_envelope($mid, $envelope_order_id): array
    {
        //用户是否已领
        $data = DB::select()
            ->from('envelope_member')
            ->where('envelope_order_id', '=', $envelope_order_id)
            ->where('memberid', '=', $mid)
            ->execute()
            ->current();
        if(!$data) {
            //领取红包
            Model_Order_Envelope::add_envelope_member($mid, $envelope_order_id);
        }

        list($list, $own, $has_max) = Model_Order_Envelope::get_envelope_show_list($envelope_order_id,$mid);
        if (empty($list)) {
            throw new Exception('红包已领完');
        }

        $ordersn = DB::select('ordersn')
            ->from('envelope_order')
            ->where('id', '=', $envelope_order_id)
            ->execute()
            ->get('ordersn');

        //订单红包信息
        $info = Model_Order_Envelope::order_show($ordersn, true);
        foreach ($list as & $item) {
            $item['addtime'] = date('Y-m-d H:i:s', $item['addtime']);
        }

        return array(
            'envelopeInfo' => $info,
            'list' => $list,
            'own' => $own,
            'hasMax' => $has_max,
        );
    }
}